<?php

class IndexAction extends Action {
    public function index(){
    	if (cookie("username")==md5(session("username"))) {
    		switch (cookie("type")){
    			case 1:
    				$this->redirect("Index/student");
    				break;
    			case 2:
    				$this->redirect("Index/teacher");
    				break;
    			default:
    				$this->display();
    		}
    	}else{
    		$this->display();
    	}
    	
    }
    public function login(){
    	$base=D("Base");
    	$login_type=$_POST["type"];
    	if ($base->checkUser()) {
    		session("username",$_POST["id"]);//将用户名写入session
    		session("type",$login_type);//将用户的登录类型写入session
    		cookie("username",md5($_POST['id']));//将用户名用md5加密后写入cookie
    		cookie("type",$login_type);//经用户的登录类型写入cookie
    		switch ($login_type){
    			case 1:
    				$this->success("用户登录成功",U('Index/student'));
    				break;
    			case 2:
    				$this->success("用户登录成功",U('Index/teacher'));
    				break;
    		}
    	
    	}else{
    		if ((int)$login_type==1 && $_POST["password"]=="123456" &&$base->checkSID($_POST["id"])) {
    			session("username",$_POST["id"]);//将用户名写入session
    			session("type",$login_type);//将用户的登录类型写入session
    			cookie("username",md5($_POST['id']));//将用户名用md5加密后写入cookie
    			cookie("type",$login_type);//经用户的登录类型写入cookie
    			$this->success("用户登录成功",U('Index/student'));
    		}else{
    			$this->error("登录出错,请重新登录");
    		}
    	}
    }
    public function student($time=0){
    	if (session("type")!=cookie("type")||session("type")!=1) {
    		$this->error("非法登录",U("Index/index"));
    	}
    	if (cookie("username")==md5(session("username"))) {
    		$urlInfo=explode("/", U("Index/teacher"));
    		for($i=2;$i>0;$i--){
    			array_pop($urlInfo);
    		}
    		$mainURL=implode("/", $urlInfo);
    		$mainURL.="/";//取得连接地址/student/index.php/
    		$this->assign("sid",session("username"));
    		$this->assign("URL",$mainURL);
    		$this->display();
    	}else{
    		$this->error("非法登录",U("Index/index"));
    	}
    	 
    }
    public function teacher($time=0){
    	if (session("type")!=2) {
    		$this->error("非法登录",U("Index/index"));
    	}
    	/* if(session("username")=="function"){
    		$this->assign("display","block");
    		$this->display();
    		exit();
    	} */
    	if (cookie("username")==md5(session("username"))) {
    		$this->assign("username",session("username"));
    		$this->assign("clientIP",$_SERVER["REMOTE_ADDR"]);
    		$check=M("Teacher");
    		$where["tid"]=session("username");
    		$where["name"]=session("username");
    		$where["_logic"]="OR";
    		$type=$check->where($where)->getField("type");
			switch((int)$type){
				case 1://教师
    				$this->assign("system","none");
					break;
				case 2://管理员
    				$this->assign("system","block");
					break;
				case 3:
    				$this->assign("teacher","none");//登录类型为3的时候，则为信息员
					$this->assign("system","none");
					break;

			}
		
    		$urlInfo=explode("/", U("Index/teacher"));
    		for($i=2;$i>0;$i--){
				array_pop($urlInfo);    			
    		}
    		$mainURL=implode("/", $urlInfo);
    		$mainURL.="/";//取得谅解地址/student/index.php/
    		$this->assign("URL",$mainURL);
    		$this->display();
    	}else{
    		$this->error("非法登录",U("Index/index"));
    	}
    }
    public function logout(){
    	session("username",null);
    	cookie("username",null);
    	$this->redirect("index");
    }
    public function createindex($type="student",$sid=""){
    	$this->assign("sid",$sid);
    	switch ($type){
    		case "student":
    			$power="display:none;";
    			$this->assign("power",$power);
    			$this->display();
    			break;
    		case "teacher":
    			echo $this->fetch();
    			break;
    		default:
    			$this->redirect("index");
    	}
    }
    public function verify(){
    	$base=D("Base");
    	$base->verify();
    }
}
